//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package weaver.interfaces.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ExpenseInfoUtil {
    //预算台账表
    private static String ysxxtable = (new BaseBean()).getPropValue("expense", "ysxxtable");
    //预算台账-课题表
    private static String ysxxtable_kt = (new BaseBean()).getPropValue("expense", "ysxxtable_kt");
    //预算台账模块id
    private static String ysxxmodeid = (new BaseBean()).getPropValue("expense", "ysxxmodeid");
    //预算台账模块id-课题
    private static String ysxxmodeid_kt = (new BaseBean()).getPropValue("expense", "ysxxmodeid_kt");
    //日志
    Log log = LogFactory.getLog(ExpenseInfoUtil.class);
    public ExpenseInfoUtil() {
    }

    public double getExpenseAmount1(String centitycode, String cexpenseitemcode, String citemcode) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select sum(amount) as amount from " + ysxxtable + " where centitycode='" + centitycode + "' and cexpenseitemcode='" + cexpenseitemcode + "' and  citemcode='" + citemcode + "'");
        rsds.next();
        double amount = Util.getDoubleValue(rsds.getString("amount"), 0.0D);
        return amount;
    }

    public double getExpenseAmount2(String cexpenseitemcode, String kth) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select sum(amount) as amount from " + ysxxtable + " where  cexpenseitemcode='" + cexpenseitemcode + "' and  kth='" + kth + "'");
        rsds.next();
        double amount = Util.getDoubleValue(rsds.getString("amount"), 0.0D);
        return amount;
    }

    public double getExpenseAmount3(String kth) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select sum(amount) as amount from " + ysxxtable + " where kth='" + kth + "'");
        rsds.next();
        double amount = Util.getDoubleValue(rsds.getString("amount"), 0.0D);
        return amount;
    }

    public double getExpenseAmount4(String cexpenseitemcode) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select sum(amount) as amount from " + ysxxtable + " where cexpenseitemcode='" + cexpenseitemcode + "'");
        rsds.next();
        double amount = Util.getDoubleValue(rsds.getString("amount"), 0.0D);
        return amount;
    }

    public double getExpenseAmount5(String ccode, String cexpenseitemcode) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select sum(amount) as amount from " + ysxxtable + " where ccode='" + ccode + "' and cexpenseitemcode='" + cexpenseitemcode + "'");
        rsds.next();
        double amount = Util.getDoubleValue(rsds.getString("amount"), 0.0D);
        return amount;
    }

    public String getKsbm(String ks) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select uf_code from cdept_dz where oa_code='" + ks + "'");
        rsds.next();
        String ksbm = Util.null2String(rsds.getString("uf_code"));
        return ksbm;
    }

    public boolean subtractExpense(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if(!"jiaoyu".equals(iskt)) {
            if ("1".equals(iskt)) {
                //sqlWwhere = sqlWwhere + " and (centitycode is null or centitycode ='')";
            } else if ("0".equals(iskt)) {
                (new BaseBean()).writeLog("不是课题===========" + iskt);
                //因为教育拨款无法扣预算，所以注释掉这一行，并不知道带来的影响是什么
                sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
            }
        }

        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)) {
            (new BaseBean()).writeLog("课题号？？==========="+kth);
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (citemcode is null or citemcode ='') ";
        }

        System.out.println("==跟新==select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "'  " + sqlWwhere);
        (new BaseBean()).writeLog("==跟新==select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "'  " + sqlWwhere);
        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);

        (new BaseBean()).writeLog("判断是否更新预算===================？");

        if(rs.next()) {
            String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
            String dcurrentamountNew = BigDecimalCalculate.floatSubtract(dcurrentamount, String.valueOf(amount));
            (new BaseBean()).writeLog("计算前预算金额："+dcurrentamount+"，本次预算扣减值："+amount+"，本次预算剩余值："+dcurrentamountNew);
            String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
            (new BaseBean()).writeLog("=========执行更新预算SQL："+updateSql);
            return rs.execute(updateSql);
        } else {
            return false;
        }
    }

    public boolean releaseExpense(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
            //sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }


    public boolean releaseExpenseKY(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
            //sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)) {
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }


    //支出凭证退回，按汇率退回！
    public boolean releaseExpenseLL(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
            //sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)) {
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }



    //试剂耗材退回方法
    public boolean releaseExpense2(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)&&"1".equals(iskt)) {
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }

    //常用试剂退回方法
    public boolean releaseExpense3(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
           // sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)&&"1".equals(iskt)) {
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }


    //院所库房退回方法
    public boolean releaseExpense4(String workflowid, String ccode, String cexpenseitemcode, String citemcode, String kth, double amount, String iyear, String iskt) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        RecordSet rs = new RecordSet();
        rsds.execute("select centitycode from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String centitycode = Util.null2String(rsds.getString("centitycode"));
        String sqlWwhere = "";

        if ("1".equals(iskt)) {
            // sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='') ";
        } else {
            sqlWwhere = sqlWwhere + " and centitycode ='" + centitycode + "'  ";
        }


        sqlWwhere = sqlWwhere + " and cexpenseitemcode ='" + cexpenseitemcode + "'  ";
        if(!"".equals(citemcode)) {
            sqlWwhere = sqlWwhere + " and citemcode ='" + citemcode + "'  ";
        }

        if(!"".equals(kth)&&"1".equals(iskt)) {
            sqlWwhere = sqlWwhere + " and centitycode ='" + kth + "'  ";
        }

        if("".equals(citemcode) && "".equals(kth)) {
            sqlWwhere = sqlWwhere + " and  (centitycode is null or citemcode ='')";
        }

        //打印SQLWHERE
        (new BaseBean()).writeLog("sqlwhere:========="+sqlWwhere);

        rs.execute("select dcurrentamount from " + ysxxtable + " where iyear='" + iyear + "' " + sqlWwhere);
        rs.next();
        String dcurrentamount = Util.null2String(rs.getString("dcurrentamount"));
        String dcurrentamountNew = BigDecimalCalculate.floatAdd(dcurrentamount, String.valueOf(amount));
        String updateSql = "update " + ysxxtable + " set dcurrentamount =" + dcurrentamountNew + " where iyear='" + iyear + "' " + sqlWwhere;
        (new BaseBean()).writeLog("退回预算执行的SQL："+updateSql);
        return rs.execute(updateSql);
    }


    public String getCname(String ccode) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select name from EF_EP_V_DEPT where code='" + ccode + "'");
        rsds.next();
        String cName = Util.null2String(rsds.getString("name"));
        return cName;
    }

    public String getCItemName(String cexpenseitemcode) {
        (new BaseBean()).writeLog("传入参数为："+cexpenseitemcode);
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
//        rsds.execute("select  distinct cexpenseitemname from EF_BG_V_ExpenseItemdz where cexpenseitemcode in (select ef_code from jfly_dz where jf_code in ('01','02','03','04','','05','06') ) and cexpenseitemcode=" + cexpenseitemcode);
        //新sql select distinct cexpenseitemname from EF_EP_itemdz where cexpenseitemcode = '101'
        rsds.execute("select distinct cexpenseitemname from EF_EP_itemdz where cexpenseitemcode = '"+cexpenseitemcode+"'");
        rsds.next();
        String cexpenseitemname = Util.null2String(rsds.getString("cexpenseitemname"));
        (new BaseBean()).writeLog("支出项目为："+cexpenseitemname);
        return cexpenseitemname;
    }

    public String getCtn(String citemcode) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select  distinct citemname from EF_BG_V_ExpenseItem where citemcode='" + citemcode + "'");
        rsds.next();
        String citemname = Util.null2String(rsds.getString("citemname"));
        return citemname;
    }

    public String getKthName(String kth) {
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        rsds.execute("select distinct citemname from EF_EP_itemdz where jfly='课题' or jfly='临床试验经费' and citemcode='" + kth + "'");
        rsds.next();
        String citemname = Util.null2String(rsds.getString("citemname"));
        return citemname;
    }

    public double getDouble2(String d) {
        BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.HALF_UP);
        return bg.doubleValue();
    }




    /**
     * 同步预算，不包含课题
     * @return
     */
    public boolean SynExpense() {
       // ((BaseBean)null).writeLog("开始同步非课题预算");
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        //RecordSetDataSource rsds2 = new RecordSetDataSource("ZJK");
        boolean bl = false;
        rsds.execute("select iyear,cbudgettype,centitycode,centityname,cexpenseitemcode,cexpenseitemname,citemcode,citemname,dBBGAmount,dsumamount,dcurrentamount from EF_EP_V_EPBudget");

        String iyear;
        String cbudgettype;
        String centitycode;
        String centityname;
        String cexpenseitemcode;
        String cexpenseitemname;
        String citemcode;
        String citemname;
        String dBBGAmount;
        String dsumamount;
        String dcurrentamount;
        String type;
        for(; rsds.next(); rs.execute("insert into updateExpenseLog(iyear,cbudgettype,centitycode,centityname,cexpenseitemcode,cexpenseitemname,citemcode,citemname,dBBGAmount,dsumamount,dcurrentamount,opdate,type)  values('" + iyear + "','" + cbudgettype + "','" + centitycode + "','" + centityname + "','" + cexpenseitemcode + "','" + cexpenseitemname + "','" + citemcode + "','" + citemname + "'," + dBBGAmount + "," + dsumamount + "," + dcurrentamount + ",'" + TimeUtil.getCurrentTimeString() + "','" + type + "')")) {
            log.error("==========================进入循环====。。。。");
            iyear = Util.null2String(rsds.getString("iyear"));
            cbudgettype = Util.null2String(rsds.getString("cbudgettype"));
            centitycode = Util.null2String(rsds.getString("centitycode"));
            centityname = Util.null2String(rsds.getString("centityname"));
            cexpenseitemcode = Util.null2String(rsds.getString("cexpenseitemcode"));
            cexpenseitemname = Util.null2String(rsds.getString("cexpenseitemname"));
            citemcode = Util.null2String(rsds.getString("citemcode"));
            citemname = Util.null2String(rsds.getString("citemname"));
            //保留两位小数
            dBBGAmount = Util.null2String(rsds.getString("dBBGAmount"));
            dsumamount =  Util.null2String(rsds.getString("dsumamount"));
            dcurrentamount =  Util.null2String(rsds.getString("dcurrentamount"));
            type = "";
            String sqlWhere = " iyear ='" + iyear + "' and cbudgettype='" + cbudgettype + "' and centitycode='" + centitycode + "' and cexpenseitemcode='" + cexpenseitemcode + "'";
            if(!"".equals(citemcode)) {
                sqlWhere = sqlWhere + " and citemcode='" + citemcode + "'";
            }

//            //判断是否需要同步
//            String isTb = "select distinct cexnamed from sales_cos where cexnamed = '"+cexpenseitemname+"'";
//            new BaseBean().writeLog("sql:"+isTb);
//            //不需要同步继续下一次循环
//            rsds2.execute(isTb);
//            if (rsds2.next()){
//                //continue;
////                continue;
//            }else{
//                continue;
//            }
//            //需要同步继续同步操作
            rs.execute("select id,dBBGAmount,dcurrentamount from " + ysxxtable + " where " + sqlWhere);
            if(rs.next()) {
                log.error("更新已有预算。。。。");
                String dBBGAmount_old = Util.null2String(rs.getString("dBBGAmount"));
                String dcurrentamount_old = Util.null2String(rs.getString("dcurrentamount"));
                String ce = BigDecimalCalculate.floatSubtract(dBBGAmount, dBBGAmount_old);
                String dcurrentamount_new = BigDecimalCalculate.floatAdd(dcurrentamount_old, ce);
                bl = rs1.execute("update " + ysxxtable + " set dBBGAmount=" + dBBGAmount + ",dsumamount=" + dsumamount + ",dcurrentamount=" + dcurrentamount_new + "  where " + sqlWhere);
            } else {
                log.error("插入新预算。。。。");
                bl = rs.execute("insert into " + ysxxtable + "(iyear,cbudgettype,centitycode,centityname,cexpenseitemcode,cexpenseitemname,citemcode,citemname,dBBGAmount,dsumamount,dcurrentamount,formmodeid,modedatacreater) " + " values('" + iyear + "','" + cbudgettype + "','" + centitycode + "','" + centityname + "','" + cexpenseitemcode + "','" + cexpenseitemname + "','" + citemcode + "','" + citemname + "'," + dBBGAmount + "," + dsumamount + "," + dcurrentamount + "," + ysxxmodeid + ",1)");
            }

            if(bl) {
                type = "1";
            } else {
                type = "0";
            }
        }

        return bl;
    }


    /*
    *同步预算 课题
     */
    public boolean SynExpenseKt() {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSetDataSource rsds = new RecordSetDataSource("ZJK");
        boolean bl = true;
        rsds.execute("select *  from EF_jc_V_EPBudget");
        String cprojectKind;//项目大类编码
        String citem_name;//项目大类名称
        String cProjectcode;//项目编码
        String cprojectname;//项目名称
        String citemcode;//收支项目编码
        String citemname;//收支项目名称
        String cSourceCode;//资金来源编码
        String sSourceName;//资金来源名称
        String dinSAmount;//预算金额
        String dkje;//到款金额

        //执行循环插入数据
        for (; rsds.next(); rs.execute("INSERT INTO updateKtExpenseLog (cprojectKind,citem_name,cProjectcode,cprojectname,citemcode,citemname,cSourceCode,sSourceName,dinSAmount,dkje) VALUES('" + cprojectKind + "','" + citem_name + "','" + cProjectcode + "','" + cprojectname + "','" + citemcode + "','" + citemname + "','" + cSourceCode + "','" + sSourceName + "','" + dinSAmount + "','" + dkje + "')")) {
            //获取字段
            cprojectKind = Util.null2String(rsds.getString("cprojectKind"));
            citem_name = Util.null2String(rsds.getString("citem_name"));
            cProjectcode = Util.null2String(rsds.getString("cProjectcode"));
            cprojectname = Util.null2String(rsds.getString("cprojectname"));
            citemcode = Util.null2String(rsds.getString("citemcode"));
            citemname = Util.null2String(rsds.getString("citemname"));
            cSourceCode = Util.null2String(rsds.getString("cSourceCode"));
            sSourceName = Util.null2String(rsds.getString("sSourceName"));
            dinSAmount = Util.null2String(rsds.getString("dinSAmount"));
            dkje = Util.null2String(rsds.getString("dkje"));
            //System.out.println("同步课题中间表数据："+cprojectKind+"=="+citem_name+"=="+cProjectcode+"=="+cprojectname+"=="+citemcode+"=="+citemname+"=="+cSourceCode+"=="+sSourceName+"=="+dinSAmount+"=="+dkje);

            //查询该课题是否存在
            String sql = "select * from uf_ystz_kt where cprojectKind = '"+cprojectKind+"' and citem_name = '"+citem_name+"' and cProjectcode = '"+cProjectcode+"'and citemcode = '"+citemcode+"' and cSourceCode = '"+cSourceCode+"'";
            rs.execute(sql);
            if (rs.next()){
                //记录老的值
                String old_dinSAmount =  Util.null2String(rs.getString("dinSAmount"));
                String old_dkje = Util.null2String(rs.getString("dkje"));;
                //存在更新预算值
                sql = "update uf_ystz_kt set dinSAmount = '"+dinSAmount+"',dkje = '"+dkje+"' where cprojectKind = '"+cprojectKind+"' and citem_name = '"+citem_name+"' and cProjectcode = '"+cProjectcode+"'and citemcode = '"+citemcode+"' and cSourceCode = '"+cSourceCode+"'";
                rs1.execute(sql);
                log.error("OA预算课题表中更新总预算："+cprojectKind+"=="+citem_name+"=="+cProjectcode+"=="+cprojectname+"=="+citemcode+"=="+citemname+"=="+cSourceCode+"=="+sSourceName+"=="+dinSAmount+"=="+dkje+"|||原预算:"+old_dinSAmount+"==="+old_dkje);
            }else {
                //不存在，插入
                sql = "INSERT INTO uf_ystz_kt (cprojectKind,citem_name,cProjectcode,cprojectname,citemcode,citemname,cSourceCode,sSourceName,dinSAmount,dkje,formmodeid,modedatacreater) VALUES('" + cprojectKind + "','" + citem_name + "','" + cProjectcode + "','" + cprojectname + "','" + citemcode + "','" + citemname + "','" + cSourceCode + "','" + sSourceName + "','" + dinSAmount + "','" + dkje + "','" +ysxxmodeid_kt + "','" +"1"+"')";
                rs1.execute(sql);
                log.error("OA预算课题表中插入数据："+cprojectKind+"=="+citem_name+"=="+cProjectcode+"=="+cprojectname+"=="+citemcode+"=="+citemname+"=="+cSourceCode+"=="+sSourceName+"=="+dinSAmount+"=="+dkje);
            }

        }
            return true;
    }


    /**
     * 该方法用于同步总预算，因为之前的表被拆分成了 两张表   所以此方法调用两次同步方法分别查询不同的表
     * @return
     */
    public boolean SynExpense_All (){
        new BaseBean().writeLog("开始同步全部数据");
        try {
            SynExpense();
            SynExpenseKt();
        }catch (Exception e){
            //出现异常？
            return false;
        }
        return true;
    }

    public Float huilvjisuan(String requestid,String tableName){
        String sql = "";
        sql = "select * from " + tableName + " where requestId=" + requestid;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        String pjbz = null;
        float pjje = 0L;
        if (rs.next()) {
            pjbz = rs.getString("pjbz");
            pjje = Util.getFloatValue(rs.getString("pjje"));
        }
        //判空和人民币
        if (null == pjbz || 0L == pjje || pjbz == "0") {
            //sothing
            //writeLog("requestId:"+requestid+"tableName"+tableName+"汇率为人民币或币种金额为空！");
            return pjje;
        }
        //开始计算货币
        sql = "select ll from uf_bzll where bzx = '"+pjbz+"'";
        rs.execute(sql);
        float ll = 0L;
        if (rs.next()) {
            ll = Util.getFloatValue(rs.getString("ll"));
        }else{
            return pjje;
        }

        //计算新金额
        float newJe = pjje * ll;
        return newJe;
    }

    public Float huilvjisuanYqsb(String requestid,String tableName){
        String sql = "";
        sql = "select * from " + tableName + " where requestId=" + requestid;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        String bizhong = null;
        float pjje = 0L;
        if (rs.next()) {
            bizhong = rs.getString("bizhong");
            pjje = Util.getFloatValue(rs.getString("zje"));
        }
        //如果是美元
        if(bizhong == null){
//            sql = "select ll from uf_bzll where bzx = '"+bizhong+"'";
            return pjje;
        }
        //开始计算货币
        sql = "select ll from uf_bzll where bzx = '"+bizhong+"'";
        rs.execute(sql);
        float ll = 0L;
        if (rs.next()) {
            ll = Util.getFloatValue(rs.getString("ll"));
        }else{
            return pjje;
        }

        //计算新金额
        float newJe = pjje * ll;
        return newJe;
    }
}
